<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Services;

use App\Exceptions\ApiException;
use App\Format\CompanyHonorDisgraceFormat;
use App\Models\CompanyHonorDisgrace;

class CompanyHonorDisgraceService
{
    public function setCompanyHonorDisgrace(CompanyHonorDisgraceFormat $companyHonorDisgraceFormat)
    {
        $id = $companyHonorDisgraceFormat->getId();
        if (empty($id)) {
            if (empty($companyHonorDisgraceFormat->getDecisionTime())) {
                $companyHonorDisgraceFormat->setDecisionTime(time());
            }
            $companyHonorDisgraceFormat->setPushTime(time());
            CompanyHonorDisgrace::query()->create($companyHonorDisgraceFormat->toArrayNotNull());
            return true;
        }
        $companyHonorDisgraceFormat->setId(null);
        CompanyHonorDisgrace::query()->where('id', $id)->update($companyHonorDisgraceFormat->toArrayNotNull());
        return true;
    }

    public function getList(int $page, int $pageSize, array $search)
    {
        $builder = CompanyHonorDisgrace::query();

        if (isset($search['start_date'], $search['end_date'])) {
            $builder->where('push_time', '>=', strtotime($search['start_date']));
            if (strlen($search['end_date']) > 10) {
                $builder->where('push_time', '<=', strtotime($search['end_date']));
            } else {
                $builder->where('push_time', '<=', strtotime($search['end_date']) + (24 * 3600));
            }
        }
        if (isset($search['start_date']) && ! isset($search['end_date'])) {
            $builder->where('push_time', '>=', strtotime($search['start_date']));
        }
        if (! isset($search['start_date']) && isset($search['end_date'])) {
            if (strlen($search['end_date']) > 10) {
                $builder->where('push_time', '<=', strtotime($search['end_date']));
            } else {
                $builder->where('push_time', '<=', strtotime($search['end_date']) + (24 * 3600));
            }
        }
        if (isset($search['company_name'])) {
            $builder->where('company_name', 'like', '%' . $search['company_name'] . '%');
        }

        if (isset($search['type'])) {
            $builder->where('type', $search['type']);
        }

        $count = $builder->count();

        $list = $builder
            ->orderByDesc('id')
            ->skip(($page - 1) * $pageSize)->take($pageSize)->get();

        return [
            'page' => $page,
            'page_size' => $pageSize,
            'count' => $count,
            'list' => $list,
        ];
    }

    public function detail(int $id)
    {
        $res = CompanyHonorDisgrace::query()->find($id);
        if (empty($res)) {
            throw new ApiException('数据不存在', 1);
        }
        return $res;
    }

    public function delete(int $id)
    {
        $res = CompanyHonorDisgrace::query()->find($id);
        if (empty($res)) {
            throw new ApiException('数据不存在', 1);
        }
        $res->delete();
        return true;
    }
}
